{
 "cells": [
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": [
    "Pygame Hello World\n",
    "\n",
    "第1步是在init()函数的帮助下导入并初始化pygame模块。"
   ],
   "id": "41c2052e59fa67dc"
  },
  {
   "metadata": {},
   "cell_type": "code",
   "source": [
    "import pygame\n",
    "pygame.init()"
   ],
   "id": "initial_id",
   "outputs": [],
   "execution_count": null
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "我们现在设置Pygame显示窗口的首选尺寸，并给它一个标题。",
   "id": "f6a4044b982b65d0"
  },
  {
   "metadata": {},
   "cell_type": "code",
   "outputs": [],
   "execution_count": null,
   "source": [
    "screen = pygame.display.set_mode((640, 480))\n",
    "pygame.display.set_caption(\"Hello World\")"
   ],
   "id": "b0db0396fbf4f751"
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "这将渲染一个游戏窗口，该窗口需要被置于一个无限的事件循环中。所有由用户交互产生的事件对象，如鼠标移动和点击等，都存储在一个事件队列中。当 pygame.QUIT 被拦截时，我们将终止事件循环。当用户点击标题栏上的CLOSE按钮时就会产生这个事件。",
   "id": "9104691dc16ba4d0"
  },
  {
   "metadata": {},
   "cell_type": "code",
   "source": [
    "while True:\n",
    "   for event in pygame.event.get():\n",
    "      if event.type == pygame.QUIT:\n",
    "         pygame.quit()"
   ],
   "id": "f6371a1cc2e2a4cb",
   "outputs": [],
   "execution_count": null
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "显示带有Hello World标题的Pygame窗口的完整代码如下",
   "id": "e05391ef19f3a5a6"
  },
  {
   "metadata": {},
   "cell_type": "code",
   "source": [
    "import pygame, sys\n",
    "\n",
    "pygame.init()\n",
    "screen = pygame.display.set_mode((640, 480))\n",
    "pygame.display.set_caption(\"Hello World\")\n",
    "while True:\n",
    "   for event in pygame.event.get():\n",
    "      if event.type == pygame.QUIT:\n",
    "         pygame.quit()\n",
    "         sys.exit()"
   ],
   "id": "28ebe893dedaf36a",
   "outputs": [],
   "execution_count": null
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": [
    "将上述脚本保存为hello.py并运行，得到以下输出结果 —\n",
    "\n",
    "Pygame - Hello World\n",
    "\n",
    "只有当点击关闭（X）按钮时，这个窗口才会被关闭。"
   ],
   "id": "7ad931c970558840"
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
